package com.ambulance.project.manager.utils;

import com.ambulance.common.exception.CustomException;
import com.ambulance.common.utils.ServletUtils;
import com.ambulance.framework.security.LoginUser;
import com.ambulance.framework.security.service.TokenService;
import com.ambulance.project.biz.entity.Dispatch;
import com.ambulance.project.biz.enums.Role;
import com.ambulance.project.biz.service.DispatchService;
import com.ambulance.project.system.domain.SysUser;
import com.ambulance.utils.CommonUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/***
 *  @description pad和Vr登陆后医生和护士信息
 *  @author zhao
 *  @date: 2020/5/15
 *  @version: v1.0
 */
@Component(value = "padCommonUtil")
public class DoctorAndNurseCommonUtil {

    @Autowired
    private TokenService tokenService;

    //获取pad登陆信息
    public PadUserLoginResult getPadLoginUser() {
        PadUserLoginResult userLoginResult = new PadUserLoginResult();
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        SysUser user = loginUser.getUser();

        userLoginResult.setUserName(user.getNickName());
        userLoginResult.setUserMark(user.getDeptId().intValue());
        userLoginResult.setUid(user.getUserId().intValue());
        userLoginResult.setHospitalName(user.getUserName());
        userLoginResult.setRole(user.getSex());
        userLoginResult.setDispatchID(user.getPid().toString());
        return userLoginResult;
    }

    public Dispatch getDispatch(){
        PadUserLoginResult padLoginUser = getPadLoginUser();
        if(CommonUtils.checkNull(padLoginUser)){
            throw  new CustomException("请重新登陆");
        }
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        Dispatch one = dispatchService.getById(loginUser.getUser().getPid());
        return one;
    }

    @Autowired
    private DispatchService dispatchService;
    private void checkUser(String role, Integer userMark, QueryWrapper queryWrapper) {
        //医生或者护士校验
        if (role.equals(Role.DOCTOR.name())) {
            queryWrapper.eq("doctorID", userMark);
        } else if (role.equals(Role.NURSE.name())) {
            queryWrapper.eq("nurseID", userMark);
        } else {
            throw new CustomException("标识不正确");
        }

    }
}
